using System;
using System.Security.Cryptography;

namespace WebApplication1.Utils
{
    /// <summary>
    /// 验证码帮助类
    /// </summary>
    public class CaptchaHelper
    {
        /// <summary>
        /// 生成4位数字验证码
        /// </summary>
        /// <returns>4位数字验证码</returns>
        public static string GenerateCaptcha()
        {
            using (var rng = RandomNumberGenerator.Create())
            {
                byte[] randomNumber = new byte[4];
                rng.GetBytes(randomNumber);
                int captchaNumber = Math.Abs(BitConverter.ToInt32(randomNumber, 0)) % 10000;
                return captchaNumber.ToString("D4");
            }
        }

        /// <summary>
        /// 验证验证码是否匹配
        /// </summary>
        /// <param name="userInput">用户输入的验证码</param>
        /// <param name="correctCaptcha">正确的验证码</param>
        /// <returns>验证结果</returns>
        public static bool ValidateCaptcha(string userInput, string correctCaptcha)
        {
            if (string.IsNullOrEmpty(userInput) || string.IsNullOrEmpty(correctCaptcha))
            {
                return false;
            }
            return userInput.Equals(correctCaptcha, StringComparison.OrdinalIgnoreCase);
        }
    }
}